﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class ConstantDeclaration : Declaration
{
    private Expression _initializer;

    internal ConstantDeclaration(SyntaxToken constKeywordToken, Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.ConstantDeclaration, constKeywordToken, name, parent)
    {
        _initializer = EmptyExpression.Default;
    }

    /// <summary>
    ///     初始化的值
    /// </summary>
    public Expression Initializer => _initializer;


    public ConstantDeclaration Update(Expression initializer)
    {
        SetChildField(ref _initializer, Expression.IsEmptyExpression, initializer);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, DeclareKeywordToken, NameIdentifier, Initializer);
    }
}


public static partial class NodeFactory
{
    public static ConstantDeclaration CreateConstantDeclaration(SyntaxNode? parent, SyntaxToken constKeywordToken,
        Identifier identifierName, Expression? initializer)
    {
        Debug.Assert(constKeywordToken.IsNode(SyntaxKinds.ConstKeywordToken));
        ConstantDeclaration decl = new(constKeywordToken, identifierName, parent);

        if (initializer != null)
        {
            decl.Update(initializer);
        }

        return decl;
    }
}